package com.ytf.test.seventh;

/**
 * @author: YTF
 * @date: 2024/9/26 14:54
 * @version: 1.0
 * @description:
 */
public class Bath {
    private String s1 = "happy",s2= "happy",s3,s4;
    private Soap soap;
    private int i;
    private float f;

    public Bath() {
        System.out.println("Bath()");
        s3 = "Joy";
        f = 3.14f;
        soap = new Soap();
    }
    {
        System.out.println("i: ");
        i=47;
    }

    public static void main(String[] args) {
        Bath bath =new Bath();
        System.out.println(bath);
        /**
         * Output:
         * i:
         * Bath()
         * Soap()
         * Bath{s1='happy', s2='happy', s3='Joy', s4='Joy', soap=Soap{s='Constructed'}, i=47, f=3.14}
         *
         * 总结：
         * 注意执行顺序，静态快比构造器先执行。这里soap 采用了惰性初始化，用到的时候才进行的初始化。
         */
    }
    @Override
    public String toString() {
        if (s4==null){
            s4 = "Joy";
        }
        return "Bath{" +
                "s1='" + s1 + '\'' +
                ", s2='" + s2 + '\'' +
                ", s3='" + s3 + '\'' +
                ", s4='" + s4 + '\'' +
                ", soap=" + soap +
                ", i=" + i +
                ", f=" + f +
                '}';
    }
}
class Soap{
    private String s;

    public Soap() {
        System.out.println("Soap()");
        s ="Constructed";
    }

    @Override
    public String toString() {
        return "Soap{" +
                "s='" + s + '\'' +
                '}';
    }
}
